using System.Data;
using DataServices;

namespace DataLayer
{
	/// <summary>
	/// Summary description for DataBase.
	/// </summary>
	public class DataBase
	{
		private static DataSet ds = new DataSet("DataSet");

		public static DataSet DataSet
		{
			get { return ds; }
			set { ds = value; }
		}

		public static DataTable Fill(DataSource dataSource, Query query)
		{
			Command command = query.Execute(dataSource);
			
			DataTable dt,dtTemp;
			DataSet dsTemp = new DataSet("dsTemp");
			
			if (! ds.Tables.Contains(query.TableName))
			{
				dt = new DataTable(query.TableName);
				dataSource.FillSchema(command, dt);
				ds.Tables.Add(dt);
			} else
				dt = ds.Tables[query.TableName];
			
			dtTemp = dt.Clone();
			dsTemp.Tables.Add(dtTemp);
			
			dataSource.Fill(command, dtTemp);
			
			ds.Merge(dsTemp);
			
			return dtTemp;
		}
		
		public static void Update(DataSource dataSource, DataTable dt, string tableName)
		{
			Command command = dataSource.CreateCommand("SELECT * FROM " + tableName + " WHERE 1=0");
			dataSource.Update(command, dt);
		}
	}
}
